package android.itv.view;

import java.util.ArrayList;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.itv.R;
import android.itv.library.ConvertChannel;
import android.itv.library.Database;
import android.itv.library.ServiceHelper;
import android.itv.library.Utils;
import android.itv.modesevice.Channel;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

public class WaitingLogin extends Activity
{
    public static  ArrayList<Channel> listChannel=new ArrayList<Channel>();
    private String user;
    private String password;
    private boolean checkRemember;
    private String account="account";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.waitinglogin);
	
	Intent intent=getIntent();
	user=intent.getStringExtra("USER");
	password=intent.getStringExtra("PASSWORD");
	checkRemember=intent.getBooleanExtra("REMEMBER", false);
	new load().execute();
    }
    
    public void startIntent()
    {
	Intent intent=new Intent(this,Main.class);
	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	startActivity(intent);
    }
    public void backIntent()
    {
	Intent intent=new Intent(this,Login.class);
	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	intent.putExtra("ERROR",true);
	
	startActivity(intent);
	finish();
    }
    
    public boolean login(String user,String password)
    {
	boolean check=true;
	try
	{	
	    listChannel=new ServiceHelper(this).getAllChannel(user, password);
	}
	catch (Exception e) 
	{
	    check=false;
	    Log.d("Ten dang nhap","Khong dung");
	}
	if(check)
	{
        	Database db=new Database(this);
        	db.saveChannels(ConvertChannel.getListChannel(listChannel));
        	Log.d("ITV","save list channel into sdcard success");
        	
        	//neu login thanh cong va chon remember thi luu lai user, pass
        	if(checkRemember)
        	{
                	SharedPreferences sp=getSharedPreferences(account, MODE_PRIVATE);
                	SharedPreferences.Editor editor=sp.edit();
                	editor.putString("USER", user);
                	editor.putString("PASS", password);
                	editor.putBoolean("CHECK", check);
                	editor.commit();
        	}
        	else
        	{
        	    	SharedPreferences sp=getSharedPreferences(account, MODE_PRIVATE);
                	SharedPreferences.Editor editor=sp.edit();
                	editor.remove("USER");
                	editor.remove("PASS");
                	editor.remove("CHECK");
                	editor.commit();
        	}
	}
	return check;	
    }
   
    
    public class load extends AsyncTask<Void, Void, Void>
    {
	boolean check;
	@Override
        protected Void doInBackground(Void... params)
        {
		 check=login(user, password);
	    return null;
        }
	@Override
	protected void onPostExecute(Void result)
	{
	    super.onPostExecute(result);
	   if(check)
		startIntent();
	    else backIntent();
		
	}
	
    }
    
    
}
